package com.cssy.xxkj.logic.web;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cssy.base.web.BaseController;
import com.cssy.base.web.BodyMsg;
import com.cssy.xxkj.basic.entity.BasicStorageInfoTestDO;
import com.cssy.xxkj.basic.service.IBasicUtilService;
import com.cssy.xxkj.logic.contants.BillStatusUtil;
import com.cssy.xxkj.logic.contants.BillTypeConstant;
import com.cssy.xxkj.logic.entity.LogicOutsideBoundItemDO;
import com.cssy.xxkj.logic.entity.LogicOutsideStockDO;
import com.cssy.xxkj.logic.entity.LogicOutsideStockItemDO;
import com.cssy.xxkj.logic.service.ILogicOutsideBoundService;
import com.cssy.xxkj.logic.service.ILogicOutsideStockService;
import com.plat.exception.SyBaseException;
import com.plat.helper.JqGridHandler;
import com.plat.helper.SearchCondition;
import com.plat.helper.SearchConditions;
import com.plat.helper.SearchResult;
import com.plat.util.GlobalResLoader;
import com.plat.util.OperationFlag;
import com.plat.util.SessionUtil;
import com.plat.util.json.OutJsonGrid;

@Controller
@RequestMapping("/logic/outsidein")
public class OutSideStockController extends BaseController<LogicOutsideStockDO> {
	
	private static final String MANAGER = "/xxkj/logic/outsidein/OutSideInManager";
	private static final String EIDT = "/xxkj/logic/outsidein/OutSideInOperation";
	
	@Resource(name = "logicOutsideStockService")
	private ILogicOutsideStockService logicOutsideStockServce;
	
	@Resource(name = "logicOutsideBoundService")
	private ILogicOutsideBoundService logicOutsideBoundService;
	
	
	@Resource(name = "basicUtilService")
	private IBasicUtilService basicUtilService;
	

	@Override
	public String manager(HttpServletRequest request, Model model)
			throws Exception {
		String zdType = request.getParameter("zdType");
		model.addAttribute("zdType", zdType);
		JqGridHandler hander = new JqGridHandler(request);
		SearchConditions scs = new SearchConditions();
		hander.getWheres(scs);
		List<BasicStorageInfoTestDO> list = basicUtilService.resultStorage(scs);
		model.addAttribute("wh",list);
		return MANAGER;
	}

	@Override
	public void list(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String zdType = request.getParameter("zdType");
		JqGridHandler hander=new JqGridHandler(request);
		SearchConditions payScs = new SearchConditions();
		hander.getWheres(payScs);
		payScs.setRemovedProperty(null);
		if("exter".equals(zdType)){
			payScs.addCondition("zd001Code", BillTypeConstant.OUTSIDEIN, SearchCondition.OP_LIKE);
		}
		if("inter".equals(zdType)){
			payScs.addCondition("zd001Code", BillTypeConstant.OUTSIDEINWH, SearchCondition.OP_LIKE);
		}
		payScs.setOrderBy("bizCreatorDate desc");
		SearchResult result= logicOutsideStockServce.findListByExample(payScs);
		OutJsonGrid out=new OutJsonGrid(result);
		out.printJSon(response);
		
	}

	@Override
	public String operationForm(HttpServletRequest request, @PathVariable String id,
			@PathVariable String flag, Model model) throws Exception {
		String zdType = request.getParameter("zdType");
		if (OperationFlag.ADD.equals(flag)) {
			// 新增单据时操作
			model.addAttribute("flag", "ADD");
		} else {
			//修改单据时操作
			LogicOutsideStockDO tsk = logicOutsideStockServce.findListById(id);
			model.addAttribute("tsk", tsk);
		}
		//查询仓库
		SearchConditions sfScs = new SearchConditions();
		sfScs.setRemovedProperty(null);
		List<BasicStorageInfoTestDO>  sgfList = basicUtilService.resultStorage(sfScs);
		model.addAttribute("sgfList", sgfList);
		//判断是外联入库还是委外后再入库
		model.addAttribute("zdType", zdType);
		return EIDT;
		
		
	}

	@Override
	public Map<String, Object> save(HttpServletRequest request,
			LogicOutsideStockDO curAP, BindingResult result, Model model)
			throws Exception {
		BodyMsg bodyMap = BodyMsg.getInstance();
		try{
			String itemdata = request.getParameter("dataitem");
			if(StringUtils.isBlank(itemdata)){
				bodyMap.isFail();
				bodyMap.configWebInfor("入库物料列表不能为空！");
				return bodyMap.modelMap;
			}
			logicOutsideStockServce.outSideInSave(curAP, request);
			bodyMap.isSuccess();
			bodyMap.configWebInfor(GlobalResLoader.getOpsuccess());
		}catch(Exception e){
			e.printStackTrace();
			throw new SyBaseException(GlobalResLoader.getOpfailed()+ e.getMessage());
		}
		return bodyMap.modelMap;
	}

	@Override
	public Map<String, Object> delete(HttpServletRequest request,
			HttpServletResponse response, Model model) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	/**
	 * 查询委外入库的item集合
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping("/gridItems")
	public void gridItems(HttpServletRequest request,HttpServletResponse response) throws Exception{
		String outSideInId = request.getParameter("outSideInId");
		if(StringUtils.isNotBlank(outSideInId)){ 
			Map<String, Object> paramMap = new HashMap<String, Object>();
			paramMap.put("outSideInId", outSideInId);
			List<LogicOutsideStockItemDO> outSideItemList = logicOutsideStockServce.findItemListByMap(paramMap);
			OutJsonGrid out = new OutJsonGrid(outSideItemList);
			out.printJSon(response);
		}
	}
	
	/**
	 * 根据委外入库单id查询委外入库单的item
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping("/outSideOutItem")
	public void outSideOutItem(HttpServletRequest request, HttpServletResponse response)throws Exception{
		String outSideOutId = request.getParameter("outSideOutId");
		if(StringUtils.isNotBlank(outSideOutId)){
			Map<String, Object> paramMap = new HashMap<String, Object>();
			paramMap.put("outSideOutId", outSideOutId);
			//查询出库单的item集合
			List<LogicOutsideBoundItemDO> outSideOutItemList = logicOutsideBoundService.findItemListByMap(paramMap);
			if(outSideOutItemList==null){
				outSideOutItemList = new ArrayList<LogicOutsideBoundItemDO>();
			}
			//封装入库单的item
			List<LogicOutsideStockItemDO> outSdInItemList = new ArrayList<LogicOutsideStockItemDO>();
			for(LogicOutsideBoundItemDO outSideOutItem : outSideOutItemList){
				if(outSideOutItem==null){
					continue;
				}
				LogicOutsideStockItemDO outSideInItem = new LogicOutsideStockItemDO();
				outSideInItem.setZd003MaterId(outSideOutItem.getZd003MaterId());
				outSideInItem.setZd004MaterCode(outSideOutItem.getZd004MaterCode());
				outSideInItem.setZd005MaterName(outSideOutItem.getZd005MaterName());
				outSideInItem.setZd006MaterType(outSideOutItem.getZd006MaterType());
				outSideInItem.setZd007MaterSpec(outSideOutItem.getZd007MaterSpec());
				outSideInItem.setZd008MaterModel(outSideOutItem.getZd008MaterModel());
				outSideInItem.setZd009MaterBrand(outSideOutItem.getZd009MaterBrand());
				outSideInItem.setZd010MaterSize(outSideOutItem.getZd010MaterSize());
				outSideInItem.setZd011MaterColor(outSideOutItem.getZd011MaterColor());
				outSideInItem.setZd012MaterQuanlity(outSideOutItem.getZd012MaterQuanlity());
				outSideInItem.setZd013Unit(outSideOutItem.getZd013Unit());
				outSideInItem.setZd014Price(outSideOutItem.getZd014Price());
				outSideInItem.setZd014TaxPrice(outSideOutItem.getZd014TaxPrice());
				//自动带出出库单的出库数量为入库单的入库数量
				BigDecimal inQuantity = outSideOutItem.getZd018OutQuantity()==null?BigDecimal.ZERO:outSideOutItem.getZd018OutQuantity();
				outSideInItem.setZd018InQuantity(inQuantity);
				outSdInItemList.add(outSideInItem);
			}
			OutJsonGrid out = new OutJsonGrid(outSdInItemList);
			out.printJSon(response);
		}
	}
	
	@RequestMapping(value = "/updateState")
	@ResponseBody
	public Map<String,String> updateState(HttpServletRequest request,
			HttpServletResponse response, Model model) throws Exception {
		Map<String, String> msgMap=new HashMap<String, String>();
		try{
			String outSideInId = request.getParameter("outSideInId");
			if(StringUtils.isNotBlank(outSideInId)){
				LogicOutsideStockDO outSideIn = logicOutsideStockServce.findListById(outSideInId);
				if(outSideIn==null||StringUtils.isBlank(outSideIn.getId())){
					msgMap.put("msg", "ERROR");
				}else{
					outSideIn.setZd001Status(BillStatusUtil.OUTSIDEIN_STATUS_THREE);
					logicOutsideStockServce.update(outSideIn, SessionUtil.getUser(request));
					msgMap.put("msg", "SUCCESS");
				}
			}
		}catch(Exception e){
			e.printStackTrace();
			msgMap.put("msg", "ERROR");
		}
		return msgMap;
	}
}
